#include "food.h"

Food::Food(btDynamicsWorld* m_dynamicsWorld, const btVector3& startOffset)
{
	settings	= Settings::Instance();
	food_size 	= settings->getCVarPtr("food_size");
	totalFrames	= 0;
	lifetime	= 0;
	energyLevel	= 0;

	color.r 	= 0.0f;
	color.g		= 0.5f;
	color.b		= 0.0f;
	color.a		= 0.5f;

	body.m_ownerWorld = m_dynamicsWorld;

	btTransform offset; offset.setIdentity();
	offset.setOrigin(startOffset);

	btTransform transform; transform.setIdentity();
	transform.setOrigin( btVector3(0.0f, 0.10f, 0.0f) );

	body.addBodyPart_Box(this, (float)*food_size/1000, (float)*food_size/1000, (float)*food_size/1000, 1.0f, offset, transform, color, FOOD);

	myMotionState = (btDefaultMotionState*)body.bodyparts[0]->body->getMotionState();
	boxShape = static_cast<btBoxShape*>(body.bodyparts[0]->shape);
	halfExtent = boxShape->getHalfExtentsWithMargin();

}

float Food::beEaten(float amount)
{
  float consumed = Bodyowner::beEaten(amount);
  if(settings->getCVar("food_scale") != 0)
  {
    float scale = energyLevel / settings->getCVar("food_maxenergy");
    //cerr << "Scaling to: " << energyLevel << "(" << scale << ")" << endl;
    body.bodyparts[0]->changeScale(scale);
  }
  return consumed;
}


void Food::draw()
{
  body.bodyparts[0]->draw();
}

Food::~Food()
{
}


